﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Data.Common;
using Data.Condition;
using Data.Enum;
using Data.Model;
using Data.ViewModel;
using DataAccess.BikeManage;
using Ninject;
using WinForm.Dialog;
using WinForm.Helper;
using WinForm.Properties;

namespace WinForm.CarManage
{
    public partial class FrmParkingManage : Form
    {
        private static readonly IKernel Kernel = new StandardKernel(new DbModule());
        private readonly IBike _bike = Kernel.Get<IBike>();
        private readonly IControlBox _controlBox = Kernel.Get<IControlBox>();
        private readonly ILockAddress _lockAddress = Kernel.Get<ILockAddress>();
        private readonly ILockArea _lockArea = Kernel.Get<ILockArea>();
        private readonly ILockPile _lockPile = Kernel.Get<ILockPile>();

        public FrmParkingManage()
        {
            InitializeComponent();
        }

        private void FrmParkingManage_Load(object sender, EventArgs e)
        {
            InitializeControls();
        }

        #region 初始化控件

        /// <summary>
        /// 初始化分页控件和下拉控件
        /// </summary>
        private void InitializeControls()
        {
            InitializeXpbArea();
            InitializeXpbControlBox();
            InitializeXpbLockAddress();
            InitializeXpbBike();
            InitializeXpbLockPile();
            InitializeComboBox();
        }

        private void InitializeXpbArea()
        {
            xpbArea.PageIndex = 1;
            xpbArea.PageSize = 20;
            xpbArea.xRefresh += RefreshAreaManage;

            xpbArea02.PageIndex = 1;
            xpbArea02.PageSize = 20;
            xpbArea02.xRefresh += RefreshAreaManage02;
        }

        private void InitializeXpbControlBox()
        {
            xpbControlBox.PageIndex = 1;
            xpbControlBox.PageSize = 20;
            xpbControlBox.xRefresh += RefreshControlBox;
        }

        private void InitializeXpbLockAddress()
        {
            xpbLockAddress.PageIndex = 1;
            xpbLockAddress.PageSize = 20;
            xpbLockAddress.xRefresh += RefreshLockAddress;
        }

        private void InitializeXpbBike()
        {
            xpbBike.PageIndex = 1;
            xpbBike.PageSize = 20;
            xpbBike.xRefresh += RefreshBike;
        }

        private void InitializeXpbLockPile()
        {
            xpbLockPile.PageIndex = 1;
            xpbLockPile.PageSize = 20;
            xpbLockPile.xRefresh += RefreshLockPile;
        }

        private void InitializeComboBox()
        {
            ResultInfo<IList<jsh_Area>> areas = _lockArea.GetLockArea(0);
            if (areas.ResultNumber == ResultEnum.Success)
            {
                var areaList = areas.Obj;
                areaList.Insert(0, jsh_Area.All());

                cmbAreaPile.DataSource = areaList;
                cmbAreaPile.DisplayMember = "Area";
                cmbAreaPile.ValueMember = "id";

                cmbAreaaddr.DataSource = areaList;
                cmbAreaaddr.DisplayMember = "Area";
                cmbAreaaddr.ValueMember = "id";
            }
        }

        #endregion

        /// <summary>
        ///     区域管理
        /// </summary>
        private void RefreshAreaManage()
        {
            string errorMessage = string.Empty;
            try
            {
                ResultInfo<PagingDataSet<LockAreaView>> areas = _lockArea.GetLockAreas(xpbArea.PageIndex, xpbArea.PageSize, txtAreaName.Text);
                if (areas.ResultNumber == ResultEnum.Success)
                {
                    SetXpbArea(areas);
                    dgvArea.DataSource = areas.Obj.Records;
                }
                if (areas.Obj != null && areas.Obj.TotalRecords == 0)
                {
                    MyMessageBox.ShowMessageBoxInfo(Resources.NotFoundDatas);
                }
            }
            catch (Exception e)
            {
                errorMessage = errorMessage + e.Message;
                MyMessageBox.ShowMessageBoxError(errorMessage);
            }
        }

        private void RefreshAreaManage02()
        {
            string errorMessage = string.Empty;
            try
            {
                ResultInfo<PagingDataSet<LockAreaView>> areas = _lockArea.GetLockAreas(xpbArea02.PageIndex, xpbArea02.PageSize, txtAreaName02.Text);
                if (areas.ResultNumber == ResultEnum.Success)
                {
                    SetXpbArea02(areas);
                    dgvArea02.DataSource = areas.Obj.Records;
                }
                if (areas.Obj != null && areas.Obj.TotalRecords == 0)
                {
                    MyMessageBox.ShowMessageBoxInfo(Resources.NotFoundDatas);
                }
            }
            catch (Exception e)
            {
                errorMessage = errorMessage + e.Message;
                MyMessageBox.ShowMessageBoxError(errorMessage);
            }
        }

        /// <summary>
        ///     更新控制箱信息
        /// </summary>
        private void RefreshControlBox()
        {
            string errorMessage = string.Empty;
            try
            {
                ResultInfo<PagingDataSet<ControlBoxView>> boxs = _controlBox.GetControlBoxs(xpbControlBox.PageIndex,
                    xpbControlBox.PageSize,
                    txtControlBoxName.Text);
                if (boxs.ResultNumber == ResultEnum.Success)
                {
                    SetXpbControlBox(boxs);
                    dgvControlBox.DataSource = boxs.Obj.Records;
                }
                else
                {
                    errorMessage = errorMessage + boxs.Describe;
                }
                if (boxs.Obj != null && boxs.Obj.TotalRecords == 0)
                {
                    MyMessageBox.ShowMessageBoxInfo(Resources.NotFoundDatas);
                    return;
                }
            }
            catch (Exception e)
            {
                errorMessage = errorMessage + e.Message;
            }
            if (!string.IsNullOrEmpty(errorMessage))
            {
                MyMessageBox.ShowMessageBoxError(errorMessage);
            }
        }

        /// <summary>
        ///     停车点刷新
        /// </summary>
        private void RefreshLockAddress()
        {
            string errorMessage = string.Empty;
            try
            {
                ResultInfo<PagingDataSet<LockAddressView>> address =
                    _lockAddress.GetLockAddress(xpbLockAddress.PageIndex,
                        xpbLockAddress.PageSize,
                        Converts.ConvertToInt(cmbAreaaddr.SelectedValue == null
                            ? ""
                            : cmbAreaaddr.SelectedValue.ToString()),
                        txtLockAddressName.Text);
                if (address.ResultNumber == ResultEnum.Success)
                {
                    SetXpbLockAddress(address);
                    dgvLockAddress.DataSource = address.Obj.Records;
                }
                else
                {
                    errorMessage = errorMessage + address.Describe;
                }
                if (address.Obj != null && address.Obj.TotalRecords == 0)
                {
                    MyMessageBox.ShowMessageBoxInfo(Resources.NotFoundDatas);
                }
            }
            catch (Exception e)
            {
                errorMessage = errorMessage + e.Message;
                MyMessageBox.ShowMessageBoxError(errorMessage);
            }
        }

        /// <summary>
        ///     更新停车柱
        /// </summary>
        private void RefreshLockPile()
        {
            string errorMessage = string.Empty;
            try
            {
                var condition = new SearchLockPileCondition
                {
                    LockPileNo = txtLockPileNo.Text,
                    LockAddressName = txtLockAddressName.Text,
                    LockAreaId = Converts.ConvertToInt(cmbAreaPile.SelectedValue.ToString())
                };
                ResultInfo<PagingDataSet<LockPileView>> piles = _lockPile.GetLockPiles(condition, xpbLockPile.PageIndex, xpbLockPile.PageSize);
                if (piles.ResultNumber == ResultEnum.Success)
                {
                    SetXpbLockPile(piles);
                    dgvLockPile.DataSource = piles.Obj.Records;
                }
                else
                {
                    errorMessage = errorMessage + piles.Describe;
                }
                if (piles.Obj != null && piles.Obj.TotalRecords == 0)
                {
                    MyMessageBox.ShowMessageBoxInfo(Resources.NotFoundDatas);
                    return;
                }
            }
            catch (Exception e)
            {
                errorMessage = errorMessage + e.Message;
            }
            if (!string.IsNullOrEmpty(errorMessage))
            {
                MyMessageBox.ShowMessageBoxError(errorMessage);
            }
        }

        /// <summary>
        ///     刷新车辆显示
        /// </summary>
        private void RefreshBike()
        {
            string errorMessage = string.Empty;
            try
            {
                ResultInfo<PagingDataSet<BikeView>> bikes = _bike.GetBikes(xpbBike.PageIndex, xpbBike.PageSize, txtBikeCode.Text, txtBikeLockAddress.Text, txtBikeControlBox.Text, 0);
                if (bikes.ResultNumber == ResultEnum.Success)
                {
                    SetXpbBike(bikes);
                    dgvBike.DataSource = bikes.Obj.Records;
                }
                else
                {
                    errorMessage = errorMessage + bikes.Describe;
                }
                if (bikes.Obj != null && bikes.Obj.TotalRecords == 0)
                {
                    MyMessageBox.ShowMessageBoxInfo(Resources.NotFoundDatas);
                    return;
                }
            }
            catch (Exception e)
            {
                errorMessage = errorMessage + e.Message;
            }
            if (!string.IsNullOrEmpty(errorMessage))
            {
                MyMessageBox.ShowMessageBoxError(errorMessage);
            }
        }

        private void SetXpbArea(ResultInfo<PagingDataSet<LockAreaView>> areas)
        {
            xpbArea.PageIndex = areas.Obj.PageIndex;
            xpbArea.PageSize = areas.Obj.PageSize;
            xpbArea.TotalRecordCount = areas.Obj.TotalRecords;
            xpbArea.Bind();
        }

        private void SetXpbArea02(ResultInfo<PagingDataSet<LockAreaView>> areas)
        {
            xpbArea02.PageIndex = areas.Obj.PageIndex;
            xpbArea02.PageSize = areas.Obj.PageSize;
            xpbArea02.TotalRecordCount = areas.Obj.TotalRecords;
            xpbArea02.Bind();
        }

        private void SetXpbControlBox(ResultInfo<PagingDataSet<ControlBoxView>> boxs)
        {
            xpbControlBox.PageIndex = boxs.Obj.PageIndex;
            xpbControlBox.PageSize = boxs.Obj.PageSize;
            xpbControlBox.TotalRecordCount = boxs.Obj.TotalRecords;
            xpbControlBox.Bind();
        }

        private void SetXpbLockAddress(ResultInfo<PagingDataSet<LockAddressView>> address)
        {
            xpbLockAddress.PageIndex = address.Obj.PageIndex;
            xpbLockAddress.PageSize = address.Obj.PageSize;
            xpbLockAddress.TotalRecordCount = address.Obj.TotalRecords;
            xpbLockAddress.Bind();
        }

        private void SetXpbLockPile(ResultInfo<PagingDataSet<LockPileView>> piles)
        {
            xpbLockPile.PageIndex = piles.Obj.PageIndex;
            xpbLockPile.PageSize = piles.Obj.PageSize;
            xpbLockPile.TotalRecordCount = piles.Obj.TotalRecords;
            xpbLockPile.Bind();
        }

        private void SetXpbBike(ResultInfo<PagingDataSet<BikeView>> bikes)
        {
            xpbBike.PageIndex = bikes.Obj.PageIndex;
            xpbBike.PageSize = bikes.Obj.PageSize;
            xpbBike.TotalRecordCount = bikes.Obj.TotalRecords;
            xpbBike.Bind();
        }

        #region LockPile

        /// <summary>
        ///     查询停车柱
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnQueryLockPile_Click(object sender, EventArgs e)
        {
            RefreshLockPile();
        }

        /// <summary>
        ///     判断右键
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dgvLockPile_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
        {
            try
            {
                if (e.Button == MouseButtons.Right)
                {
                    if (e.RowIndex >= 0)
                    {
                        dgvLockPile.ClearSelection();
                        dgvLockPile.Rows[e.RowIndex].Selected = true;
                        dgvLockPile.CurrentCell = dgvLockPile.Rows[e.RowIndex].Cells[e.ColumnIndex];
                        cmsLockPile.Show(MousePosition.X, MousePosition.Y);
                    }
                }
            }
            catch (Exception)
            {
                MyMessageBox.ShowMessageBoxError(Resources.Tis_SelectRow);
            }
        }

        /// <summary>
        ///     新增停车柱
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAddLockPile_Click(object sender, EventArgs e)
        {
            EditLockPile lockPile = GenerateLockPileDialog(null);
            if (lockPile.DialogResult == DialogResult.OK)
            {
                CloseEditLockPileDialog(lockPile);
            }
        }

        /// <summary>
        ///     更新停车柱
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void updateLockPile_Click(object sender, EventArgs e)
        {
            string errorMessage = string.Empty;
            try
            {
                int pileId = Convert.ToInt32(dgvLockPile.SelectedRows[0].Cells["pileid"].Value);
                EditLockPile lockPile = GenerateLockPileDialog(pileId);
                if (lockPile.DialogResult == DialogResult.OK)
                {
                    CloseEditLockPileDialog(lockPile);
                }
            }
            catch (Exception)
            {
                errorMessage = Resources.Tis_SelectRow;
            }
            if (!string.IsNullOrEmpty(errorMessage))
            {
                MyMessageBox.ShowMessageBoxError(errorMessage);
            }
        }

        private static EditLockPile GenerateLockPileDialog(int? pileId)
        {
            EditLockPile lockPile;
            if (pileId != null && pileId.Value > 0)
            {
                lockPile = new EditLockPile(pileId.Value);
            }
            else
            {
                lockPile = new EditLockPile();
            }
            lockPile.StartPosition = FormStartPosition.CenterScreen;
            lockPile.ShowDialog();
            return lockPile;
        }

        /// <summary>
        ///     删除停车柱
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void deleteLockPile_Click(object sender, EventArgs e)
        {
            string errorMessage = string.Empty;
            try
            {
                object lockNo = dgvLockPile.SelectedRows[0].Cells["lockno"].Value;
                int addressId = Convert.ToInt32(dgvLockPile.SelectedRows[0].Cells["pileid"].Value);
                if (MyMessageBox.ShowMessageBoxQuestion("是否删除锁柱" + lockNo + "?") == DialogResult.Yes)
                {
                    ResultInfo<bool> result = _lockPile.DeleteLockPile(addressId);
                    if (result.ResultNumber == ResultEnum.Success && result.Obj)
                    {
                        errorMessage = "删除锁柱" + lockNo + "成功";
                        RefreshLockPile();
                    }
                    else
                    {
                        errorMessage = result.Describe;
                    }
                }
            }
            catch (Exception)
            {
                errorMessage = errorMessage + Resources.Tis_SelectRow;
            }
            if (!string.IsNullOrEmpty(errorMessage))
            {
                MyMessageBox.ShowMessageBoxError(errorMessage);
            }
        }

        private void CloseEditLockPileDialog(EditLockPile lockPile)
        {
            txtLockPileNo.Clear();
            RefreshLockPile();
            lockPile.Close();
            lockPile.Dispose();
        }

        #endregion LockPile

        #region Area

        /// <summary>
        ///     查询停车区域
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnQueryArea_Click(object sender, EventArgs e)
        {
            RefreshAreaManage();
        }

        /// <summary>
        ///     删除选中区域
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void deleteAare_Click(object sender, EventArgs e)
        {
            string errorMessage = string.Empty;
            try
            {
                int areaId = Convert.ToInt32(dgvArea.SelectedRows[0].Cells["id"].Value);
                if (
                    MyMessageBox.ShowMessageBoxQuestion("是否删除停车区域" + dgvArea.SelectedRows[0].Cells["name"].Value + "？") ==
                    DialogResult.Yes)
                {
                    ResultInfo<bool> result = _lockArea.DeleteLockArea(areaId);
                    if (result.ResultNumber == ResultEnum.Success && result.Obj)
                    {
                        errorMessage = "操作成功";
                        RefreshAreaManage();
                    }
                    else
                    {
                        errorMessage = result.Describe;
                    }
                }
            }
            catch (Exception)
            {
                errorMessage = errorMessage + Resources.Tis_SelectRow;
            }
            if (!string.IsNullOrEmpty(errorMessage))
            {
                MyMessageBox.ShowMessageBoxError(errorMessage);
            }
        }

        /// <summary>
        ///     更新选中区域
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void updateArea_Click(object sender, EventArgs e)
        {
            string errorMessage = string.Empty;
            try
            {
                int areaId = Convert.ToInt32(dgvArea.SelectedRows[0].Cells["id"].Value);
                EditLockArea lockArea = GenerateLockAreaDialog(areaId);
                if (lockArea.DialogResult == DialogResult.OK)
                {
                    CloseEditLockAreaDialog(lockArea);
                }
            }
            catch (Exception e1)
            {
                errorMessage = "请先选择一条记录再操作";
            }
            if (!string.IsNullOrEmpty(errorMessage))
            {
                MyMessageBox.ShowMessageBoxError(errorMessage);
            }
        }

        private static EditLockArea GenerateLockAreaDialog(int? areaId)
        {
            EditLockArea lockArea;
            if (areaId != null && areaId.Value > 0)
            {
                lockArea = new EditLockArea(areaId.Value);
            }
            else
            {
                lockArea = new EditLockArea();
            }
            lockArea.StartPosition = FormStartPosition.CenterParent;
            lockArea.ShowDialog();
            return lockArea;
        }

        private void CloseEditLockAreaDialog(EditLockArea lockArea)
        {
            txtAreaName.Clear();
            RefreshAreaManage();
            lockArea.Close();
            lockArea.Dispose();
        }

        private static EditLockArea GenerateLockAreaDialog02(int? areaId)
        {
            EditLockArea lockArea;
            if (areaId != null && areaId.Value > 0)
            {
                lockArea = new EditLockArea(areaId.Value);
            }
            else
            {
                lockArea = new EditLockArea();
            }
            lockArea.StartPosition = FormStartPosition.CenterParent;
            lockArea.ShowDialog();
            return lockArea;
        }

        private void CloseEditLockAreaDialog02(EditLockArea lockArea)
        {
            txtAreaName02.Clear();
            RefreshAreaManage02();
            lockArea.Close();
            lockArea.Dispose();
        }


        /// <summary>
        ///     新加一个停车区域
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAddArea_Click(object sender, EventArgs e)
        {
            EditLockArea lockArea = GenerateLockAreaDialog(null);
            if (lockArea.DialogResult == DialogResult.OK)
            {
                CloseEditLockAreaDialog(lockArea);
            }
        }

        /// <summary>
        ///     删除选中区域
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnDeleteArea_Click(object sender, EventArgs e)
        {
            string errorMessage = string.Empty;
            try
            {
                int areaid = Convert.ToInt32(dgvArea.SelectedRows[0].Cells["id"].Value);
                if (
                    MyMessageBox.ShowMessageBoxQuestion("是否删除停车区域" + dgvArea.SelectedRows[0].Cells["name"].Value + "？") ==
                    DialogResult.Yes)
                {
                    ResultInfo<bool> result = _lockArea.DeleteLockArea(areaid);
                    if (result.ResultNumber == ResultEnum.Success && result.Obj)
                    {
                        MyMessageBox.ShowMessageBoxInfo("操作成功！");
                        RefreshAreaManage();
                    }
                    else
                    {
                        errorMessage = result.Describe;
                    }
                }
            }
            catch (Exception e1)
            {
                errorMessage = errorMessage + "请先选择一条记录再操作";
            }
            if (!string.IsNullOrEmpty(errorMessage))
            {
                MyMessageBox.ShowMessageBoxError(errorMessage);
            }
        }

        /// <summary>
        ///     更新停车区域
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnUpdateArea_Click(object sender, EventArgs e)
        {
            string errorMessage = string.Empty;
            try
            {
                int areaId = Convert.ToInt32(dgvArea.SelectedRows[0].Cells["id"].Value);
                EditLockArea lockArea = GenerateLockAreaDialog(areaId);
                if (lockArea.DialogResult == DialogResult.OK)
                {
                    CloseEditLockAreaDialog(lockArea);
                }
            }
            catch (Exception)
            {
                errorMessage = "请先选择一条记录再操作";
            }
            if (!string.IsNullOrEmpty(errorMessage))
            {
                MyMessageBox.ShowMessageBoxError(errorMessage);
            }
        }

        /// <summary>
        ///     判断右键
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dgvArea_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
        {
            try
            {
                if (e.Button == MouseButtons.Right)
                {
                    if (e.RowIndex >= 0)
                    {
                        dgvArea.ClearSelection();
                        dgvArea.Rows[e.RowIndex].Selected = true;
                        dgvArea.CurrentCell = dgvArea.Rows[e.RowIndex].Cells[e.ColumnIndex];
                        cmsArea.Show(MousePosition.X, MousePosition.Y);
                    }
                }
            }
            catch (Exception)
            {
                MyMessageBox.ShowMessageBoxError(Resources.Tis_SelectRow);
            }
        }

        #endregion

        #region Controlbox

        /// <summary>
        ///     控制箱查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnQueryControlBox_Click(object sender, EventArgs e)
        {
            RefreshControlBox();
        }

        /// <summary>
        ///     新增
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAddControlbox_Click(object sender, EventArgs e)
        {
            var controlBox = new EditControlBox { StartPosition = FormStartPosition.CenterParent };
            controlBox.ShowDialog();
            if (controlBox.DialogResult == DialogResult.OK)
            {
                txtControlBoxName.Clear();
                RefreshControlBox();
                controlBox.Close();
                controlBox.Dispose();
            }
        }

        /// <summary>
        ///     更新控制箱
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void updateControlBox_Click(object sender, EventArgs e)
        {
            string errorMessage = string.Empty;
            try
            {
                int controlBoxId = Convert.ToInt32(dgvControlBox.SelectedRows[0].Cells["controlboxid"].Value);
                var controlBox = new EditControlBox(controlBoxId) { StartPosition = FormStartPosition.CenterParent };
                controlBox.ShowDialog();
                if (controlBox.DialogResult == DialogResult.OK)
                {
                    txtControlBoxName.Clear();
                    RefreshControlBox();
                    controlBox.Close();
                    controlBox.Dispose();
                }
            }
            catch (Exception)
            {
                errorMessage = "请先选择一条记录再操作";
            }
            if (!string.IsNullOrEmpty(errorMessage))
            {
                MyMessageBox.ShowMessageBoxError(errorMessage);
            }
        }

        /// <summary>
        ///     删除控制箱
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void deleteControlBox_Click(object sender, EventArgs e)
        {
            string errorMessage = string.Empty;
            try
            {
                int controlBoxid = Convert.ToInt32(dgvControlBox.SelectedRows[0].Cells["controlboxid"].Value);
                if (
                    MyMessageBox.ShowMessageBoxQuestion("是否删除控制箱" + dgvControlBox.SelectedRows[0].Cells["boxno"].Value +
                                                        "？") == DialogResult.Yes)
                {
                    ResultInfo<bool> result = _controlBox.DeleteControlBox(controlBoxid);
                    if (result.ResultNumber == ResultEnum.Success && result.Obj)
                    {
                        errorMessage = "操作成功";
                        RefreshControlBox();
                    }
                    else
                    {
                        errorMessage = result.Describe;
                    }
                }
            }
            catch (Exception)
            {
                errorMessage = errorMessage + "请先选择一条记录再操作";
            }
            if (!string.IsNullOrEmpty(errorMessage))
            {
                MyMessageBox.ShowMessageBoxError(errorMessage);
            }
        }

        /// <summary>
        ///     删除控制箱
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnDeleteControlBox_Click(object sender, EventArgs e)
        {
            string errorMessage = string.Empty;
            try
            {
                int controlBoxId = Convert.ToInt32(dgvControlBox.SelectedRows[0].Cells["controlboxid"].Value);
                if (
                    MyMessageBox.ShowMessageBoxQuestion("是否删除控制箱" + dgvControlBox.SelectedRows[0].Cells["boxno"].Value +
                                                        "？") == DialogResult.Yes)
                {
                    ResultInfo<bool> result = _controlBox.DeleteControlBox(controlBoxId);
                    if (result.ResultNumber == ResultEnum.Success && result.Obj)
                    {
                        errorMessage = "操作成功";
                        RefreshControlBox();
                    }
                    else
                    {
                        errorMessage = result.Describe;
                    }
                }
            }
            catch (Exception)
            {
                errorMessage = errorMessage + "请先选择一条记录再操作";
            }
            if (!string.IsNullOrEmpty(errorMessage))
            {
                MyMessageBox.ShowMessageBoxError(errorMessage);
            }
        }

        /// <summary>
        ///     修改控制箱
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnUpdateControlbox_Click(object sender, EventArgs e)
        {
            string errorMessage = string.Empty;
            try
            {
                int controlBoxId = Convert.ToInt32(dgvControlBox.SelectedRows[0].Cells["controlboxid"].Value);
                var controlBox = new EditControlBox(controlBoxId) { StartPosition = FormStartPosition.CenterParent };
                controlBox.ShowDialog();
                if (controlBox.DialogResult == DialogResult.OK)
                {
                    txtControlBoxName.Clear();
                    RefreshControlBox();
                    controlBox.Close();
                    controlBox.Dispose();
                }
            }
            catch (Exception)
            {
                errorMessage = "请先选择一条记录再操作";
            }
            if (!string.IsNullOrEmpty(errorMessage))
            {
                MyMessageBox.ShowMessageBoxError(errorMessage);
            }
        }

        /// <summary>
        ///     控制箱右键弹出菜单
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dgvControlBox_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
        {
            try
            {
                if (e.Button == MouseButtons.Right)
                {
                    if (e.RowIndex >= 0)
                    {
                        dgvControlBox.ClearSelection();
                        dgvControlBox.Rows[e.RowIndex].Selected = true;
                        dgvControlBox.CurrentCell = dgvControlBox.Rows[e.RowIndex].Cells[e.ColumnIndex];
                        cmsControlBox.Show(MousePosition.X, MousePosition.Y);
                    }
                }
            }
            catch (Exception)
            {
                MyMessageBox.ShowMessageBoxError(Resources.Tis_SelectRow);
            }
        }

        #endregion

        #region Bike

        /// <summary>
        ///     新增车辆
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAddBike_Click(object sender, EventArgs e)
        {
            var editBike = new EditBike { StartPosition = FormStartPosition.CenterParent };
            editBike.ShowDialog();
            if (editBike.DialogResult == DialogResult.OK)
            {
                RefreshBike();
                editBike.Close();
                editBike.Dispose();
            }
        }

        /// <summary>
        ///     查询车辆
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnQueryBike_Click(object sender, EventArgs e)
        {
            RefreshBike();
        }

        /// <summary>
        ///     删除车辆信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsmDeleteBike_Click(object sender, EventArgs e)
        {
            string errorMessage = string.Empty;
            try
            {
                int bikeId = Convert.ToInt32(dgvBike.SelectedRows[0].Cells["BikeId"].Value);
                if (
                    MyMessageBox.ShowMessageBoxQuestion("是否删除车辆" + dgvBike.SelectedRows[0].Cells["BikeCode"].Value + "？") ==
                    DialogResult.Yes)
                {
                    var db = new Bike();
                    ResultInfo<bool> result = db.DeleteBike(bikeId);
                    if (result.ResultNumber == ResultEnum.Success && result.Obj)
                    {
                        errorMessage = "操作成功";
                        RefreshBike();
                    }
                    else
                    {
                        errorMessage = result.Describe;
                    }
                }
            }
            catch (Exception)
            {
                errorMessage = errorMessage + "请先选择一条记录再操作";
            }
            if (!string.IsNullOrEmpty(errorMessage))
            {
                MyMessageBox.ShowMessageBoxError(errorMessage);
            }
        }

        /// <summary>
        ///     更新车辆信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsmUpdateBike_Click(object sender, EventArgs e)
        {
            string errorMessage = string.Empty;
            try
            {
                int bikeId = Convert.ToInt32(dgvBike.SelectedRows[0].Cells["BikeId"].Value);
                var editBike = new EditBike(bikeId) { StartPosition = FormStartPosition.CenterParent };
                editBike.ShowDialog();
                if (editBike.DialogResult == DialogResult.OK)
                {
                    RefreshBike();
                    editBike.Close();
                    editBike.Dispose();
                }
            }
            catch (Exception)
            {
                errorMessage = "请先选择一条记录再操作";
            }
            if (!string.IsNullOrEmpty(errorMessage))
            {
                MyMessageBox.ShowMessageBoxError(errorMessage);
            }
        }

        #endregion

        #region LockAddress

        /// <summary>
        ///     查询停车点
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnQueryLockAddress_Click(object sender, EventArgs e)
        {
            RefreshLockAddress();
        }

        /// <summary>
        ///     新增停车点
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAddLockAddress_Click(object sender, EventArgs e)
        {
            var lockAddress = new EditLockAddress();
            lockAddress.StartPosition = FormStartPosition.CenterParent;
            lockAddress.ShowDialog();
            if (lockAddress.DialogResult == DialogResult.OK)
            {
                txtLockAddressName.Clear();
                RefreshLockAddress();
                lockAddress.Close();
                lockAddress.Dispose();
            }
        }

        /// <summary>
        ///     更新停车点
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void updateLockAddress_Click(object sender, EventArgs e)
        {
            string errorMessage = string.Empty;
            try
            {
                int addressid = Convert.ToInt32(dgvLockAddress.SelectedRows[0].Cells["addressid"].Value);
                var lockAddress = new EditLockAddress(addressid);
                lockAddress.ShowDialog();
                lockAddress.StartPosition = FormStartPosition.CenterParent;
                if (lockAddress.DialogResult == DialogResult.OK)
                {
                    txtLockAddressName.Clear();
                    RefreshLockAddress();
                    lockAddress.Close();
                    lockAddress.Dispose();
                }
            }
            catch (Exception e1)
            {
                errorMessage = "请先选择一条记录再操作";
            }
            if (!string.IsNullOrEmpty(errorMessage))
            {
                MyMessageBox.ShowMessageBoxError(errorMessage);
            }
        }

        /// <summary>
        ///     删除停车点
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void deleteLockAddress_Click(object sender, EventArgs e)
        {
            string errorMessage = string.Empty;
            try
            {
                int addressId = Convert.ToInt32(dgvLockAddress.SelectedRows[0].Cells["addressid"].Value);
                if (
                    MyMessageBox.ShowMessageBoxQuestion("是否删除停车点" +
                                                        dgvLockAddress.SelectedRows[0].Cells["addressname"].Value + "?") ==
                    DialogResult.Yes)
                {
                    ResultInfo<bool> result = _lockAddress.DeleteLockAddress(addressId);
                    if (result.ResultNumber == ResultEnum.Success && result.Obj)
                    {
                        errorMessage = "操作成功";
                        RefreshLockAddress();
                    }
                    else
                    {
                        errorMessage = result.Describe;
                    }
                }
            }
            catch (Exception e1)
            {
                errorMessage = errorMessage + "请先选择一条记录再操作";
            }
            if (!string.IsNullOrEmpty(errorMessage))
            {
                MyMessageBox.ShowMessageBoxError(errorMessage);
            }
        }

        /// <summary>
        ///     右键
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dgvLockAddress_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
        {
            try
            {
                if (e.Button == MouseButtons.Right)
                {
                    if (e.RowIndex >= 0)
                    {
                        dgvLockAddress.ClearSelection();
                        dgvLockAddress.Rows[e.RowIndex].Selected = true;
                        dgvLockAddress.CurrentCell = dgvLockAddress.Rows[e.RowIndex].Cells[e.ColumnIndex];
                        cmsLockAddress.Show(MousePosition.X, MousePosition.Y);
                    }
                }
            }
            catch (Exception)
            {
                MyMessageBox.ShowMessageBoxError(Resources.Tis_SelectRow);
            }
        }

        #endregion

        #region clone

        private void btnQueryArea02_Click(object sender, EventArgs e)
        {
            RefreshAreaManage02();
        }

        private void btnAddArea02_Click(object sender, EventArgs e)
        {
            EditLockArea lockArea = GenerateLockAreaDialog02(null);
            if (lockArea.DialogResult == DialogResult.OK)
            {
                CloseEditLockAreaDialog02(lockArea);
            }
        }

        private void btnDeleteArea02_Click(object sender, EventArgs e)
        {
            string errorMessage = string.Empty;
            try
            {
                int areaid = Convert.ToInt32(dgvArea02.SelectedRows[0].Cells["id02"].Value);
                if (
                    MyMessageBox.ShowMessageBoxQuestion("是否删除网点" + dgvArea02.SelectedRows[0].Cells["name02"].Value + "？") ==
                    DialogResult.Yes)
                {
                    ResultInfo<bool> result = _lockArea.DeleteLockArea(areaid);
                    if (result.ResultNumber == ResultEnum.Success && result.Obj)
                    {
                        MyMessageBox.ShowMessageBoxInfo("操作成功！");
                        RefreshAreaManage02();
                    }
                    else
                    {
                        errorMessage = result.Describe;
                    }
                }
            }
            catch (Exception e1)
            {
                errorMessage = errorMessage + "请先选择一条记录再操作";
            }
            if (!string.IsNullOrEmpty(errorMessage))
            {
                MyMessageBox.ShowMessageBoxError(errorMessage);
            }
        }

        private void btnUpdateArea02_Click(object sender, EventArgs e)
        {
            string errorMessage = string.Empty;
            try
            {
                int areaId = Convert.ToInt32(dgvArea02.SelectedRows[0].Cells["id02"].Value);
                EditLockArea lockArea = GenerateLockAreaDialog02(areaId);
                if (lockArea.DialogResult == DialogResult.OK)
                {
                    CloseEditLockAreaDialog02(lockArea);
                }
            }
            catch (Exception)
            {
                errorMessage = "请先选择一条记录再操作";
            }
            if (!string.IsNullOrEmpty(errorMessage))
            {
                MyMessageBox.ShowMessageBoxError(errorMessage);
            }
        }

        #endregion

        private void superTabItem1_Click(object sender, EventArgs e)
        {
            
        }

        
    }
}